package com.asj.receiver;

import static com.asj.enums.DeviceIdentifierDictEnums.actionPackageAdded;
import static com.asj.enums.DeviceIdentifierDictEnums.actionPackageRemoved;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_appInfo;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_appOption;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_deviceCode;
import static com.asj.enums.HttpRequestDictEnums.receiveNewAppInfoUrl;
import static com.asj.enums.ResultCodeEnums.request_error;
import static com.asj.enums.ResultCodeEnums.result_success;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.asj.MainActivity;
import com.asj.service.binding.AppInfoHandlerService;
import com.asj.service.oppoSdk.OPPOSDKService;
import com.asj.utils.CommonsUtils;
import com.asj.utils.QiNiuYunUploader;
import com.google.gson.JsonObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Objects;

/**
 * 监听应用安装卸载
 * */
public class AppInstallReceiver extends BroadcastReceiver {
    private static AppInstallReceiver appInstallReceiver;

    @Override
    public void onReceive(Context context, Intent intent) {
        //应用包名
        String packageName = Objects.requireNonNull(intent.getData()).getSchemeSpecificPart();
        //安装新应用
        if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
            AppInfoHandlerService.getInstance(context).transientAppInfo(packageName,1);
        }
        //卸载应用
        else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
            AppInfoHandlerService.getInstance(context).transientAppInfo(packageName,2);
        }
       /* PackageManager manager = context.getPackageManager();
        if (Objects.equals(intent.getAction(), Intent.ACTION_PACKAGE_REPLACED)) {
            String packageName = Objects.requireNonNull(intent.getData()).getSchemeSpecificPart();
            Toast.makeText(context, "替换成功"+packageName, Toast.LENGTH_LONG).show();
        }
        if (Objects.equals(intent.getAction(), Intent.ACTION_PACKAGE_CHANGED)) {
            String packageName = Objects.requireNonNull(intent.getData()).getSchemeSpecificPart();
            Toast.makeText(context, "已改变"+packageName, Toast.LENGTH_LONG).show();
        }
        if (Objects.equals(intent.getAction(), Intent.ACTION_PACKAGE_RESTARTED)) {
            String packageName = Objects.requireNonNull(intent.getData()).getSchemeSpecificPart();
            Toast.makeText(context, "重新开始"+packageName, Toast.LENGTH_LONG).show();
        }
        if (Objects.equals(intent.getAction(), Intent.ACTION_PACKAGE_DATA_CLEARED)) {
            String packageName = Objects.requireNonNull(intent.getData()).getSchemeSpecificPart();
            Toast.makeText(context, "清除包"+packageName, Toast.LENGTH_LONG).show();
        }*/
    }

    /**
     * 设立应用装卸监听器
     * */
    public static void settingAppInstallReceiver(Context context) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        filter.addDataScheme("package"); // 表示对所有包管理相关的Intent感兴趣
        appInstallReceiver = new AppInstallReceiver();
        context.registerReceiver(appInstallReceiver, filter);
        Log.w("应用装卸监听器启动","启动start");
    }

    /**
     * 移除应用装卸监听器
     */
    public static void removeAppInstallReceiver(Context context) {
        if (appInstallReceiver != null) {
            context.unregisterReceiver(appInstallReceiver);
            Log.w("应用装卸监听器关闭","close");
        }
    }
}